-- 帝王蟹，天体，织影解锁的东西用解剖不好弄，搞个死亡事件给周围的酷库德解锁。
-- 雨：定义一个解锁配方的函数，避免一直重复不必要的代码块
local function TryUnlockRecipe(learner, recipe)
    if learner ~= nil and learner.components.builder ~= nil
    and not learner.components.builder:KnowsRecipe(recipe) then
        learner.components.builder:UnlockRecipe(recipe)
        learner:PushEvent("learnrecipe", { teacher = learner, recipe = recipe })    -- 雨：这里不要推配方，推prefab就行，应该是去获取图片的
    end
end

local function FindAndUnlockDarkTrade(inst, data)
    if data ~= nil and data.cause ~= nil and data.afflicter ~= nil then
        local x,y,z = inst.Transform:GetWorldPosition()
        local kukudes = TheSim:FindEntities(x,y,z, 25, { "kukude" })
        for i,v in pairs(kukudes) do
            TryUnlockRecipe(v, "io_darktrade")
        end
    end
end

local function CountGemNum(inst)
    local gemList = {}
    if inst and inst.socketed then
        for _,g in pairs(inst.socketed or {}) do
            local gem = g["itemprefab"]
            if gemList[gem] == nil then
                gemList[gem] = 1
            else
                gemList[gem] = gemList[gem] + 1
            end
        end
    end
    return gemList
end

local function FindAndUnlockCrabThings(inst, data)
    if data ~= nil and data.cause ~= nil and data.afflicter ~= nil then
        local gemList = CountGemNum(inst)
        local x,y,z = inst.Transform:GetWorldPosition()
        local kukudes = TheSim:FindEntities(x,y,z, 25, { "kukude" })
        for i,v in pairs(kukudes) do
            -- 强化增生心脏（红宝石）
            if gemList["redgem"] and gemList["redgem"] >= 2 then
                TryUnlockRecipe(v, "io_heart_enhanced")
            end
            -- 冰封之心（蓝宝石）
            if gemList["bluegem"] and gemList["bluegem"] >= 2 then
                TryUnlockRecipe(v, "io_frozenheart")
            end
            -- 噩梦附肢（紫宝石）
            if gemList["purplegem"] and gemList["purplegem"] >= 2 then
                TryUnlockRecipe(v, "io_artus_nightmare")
            end
            -- 敏捷附肢（橙宝石）
            if gemList["orangegem"] and gemList["orangegem"] >= 2 then
                TryUnlockRecipe(v, "io_artus_agility")
            end
            -- 外置大脑PLUS（黄宝石）
            if gemList["yellowgem"] and gemList["yellowgem"] >= 2 then
                TryUnlockRecipe(v, "io_outbrain_plus")
            end
            -- 巧手附肢（绿宝石）
            if gemList["greengem"] and gemList["greengem"] >= 2 then
                TryUnlockRecipe(v, "io_artus_handy")
            end
            -- 能量附肢（彩虹宝石）
            if gemList["opalpreciousgem"] and gemList["opalpreciousgem"] >= 2 then
                TryUnlockRecipe(v, "io_artus_energy")
            end
            -- 碎心者（珍珠）
            if gemList["hermit_pearl"] and gemList["hermit_pearl"] >= 1 then
                TryUnlockRecipe(v, "io_heartbreaker")
            end
        end
    end
end

local function FindAndUnlockHeavenEye(inst, data)
    if data ~= nil and data.cause ~= nil and data.afflicter ~= nil then
        local x,y,z = inst.Transform:GetWorldPosition()
        local kukudes = TheSim:FindEntities(x,y,z, 25, { "kukude" })
        for i,v in pairs(kukudes) do
            TryUnlockRecipe(v, "io_heaveneye")
        end
    end
end

local function FindAndUnlockOutBrainULTRA(inst, data)
    if data ~= nil and data.cause ~= nil and data.afflicter ~= nil then
        local x,y,z = inst.Transform:GetWorldPosition()
        local kukudes = TheSim:FindEntities(x,y,z, 25, { "kukude" })
        for i,v in pairs(kukudes) do
            TryUnlockRecipe(v, "io_outbrain_ultra")
        end
    end
end

AddPrefabPostInit("stalker_atrium", function(inst)
    if not TheWorld.ismastersim then return end
    inst:ListenForEvent("death", FindAndUnlockDarkTrade)
end)

AddPrefabPostInit("crabking", function(inst)
    if not TheWorld.ismastersim then return end
    inst:ListenForEvent("death", FindAndUnlockCrabThings)
end)

AddPrefabPostInit("alterguardian_phase2", function(inst)
    if not TheWorld.ismastersim then return end
    inst:ListenForEvent("death", FindAndUnlockHeavenEye)
end)

AddPrefabPostInit("alterguardian_phase3", function(inst)
    if not TheWorld.ismastersim then return end
    inst:ListenForEvent("death", FindAndUnlockOutBrainULTRA)
end)
